

using System;
using System.Collections.Generic;
using System.Text;
using System.Web.Security;

namespace OrbitOne.SharePoint.Provision.WebParts
{
    class SQLProvisioning
    {
        /// <summary>
        /// Create a new user in the membership directory
        /// </summary>
        /// <param name="login">The user name for the new user.</param>
        /// <param name="password">The password for the new user.</param>
        /// <param name="email">The e-mail address for the new user.</param>
        /// <param name="passwordQuestion">The password-question value for the membership user.</param>
        /// <param name="passwordAnswer">The password-answer value for the membership user.</param>
        /// <returns>Message describing the status of the status of the creation process</returns>
        public static void CreateUser(string login, string password, string email, string passwordQuestion, string passwordAnswer)
        {
            string strStatus = String.Empty;
            MembershipCreateStatus userStatus = new MembershipCreateStatus();

            try
            {
                // Create the user in the SQL membership database
                if(Membership.Provider.RequiresQuestionAndAnswer)
                    Membership.CreateUser(login, password, email, passwordQuestion, passwordAnswer, true, out userStatus);
                else
                    Membership.CreateUser(login, password, email, null, null, true, out userStatus);

            }
            catch (Exception ex)
            {
                strStatus = "User creation failed: " + ex.Message;
                throw new Exception(strStatus);
            }

            // Check the Status
            if (userStatus != MembershipCreateStatus.Success)
            {
                strStatus = "User creation failed: " + userStatus.ToString();
                throw new Exception(strStatus);
            }
        }

        /// <summary>
        /// Delete a user from the membership directory
        /// </summary>
        /// <param name="UserName">The user name of the user to delete</param>
        public static void DeleteUser(string UserName)
        {
            bool bDeleted = false;

            try
            {
                bDeleted = Membership.DeleteUser(UserName, true);
                if (bDeleted == false) throw new Exception("The user could not be deleted.");
            }
            catch (Exception ex)
            {
                throw new Exception("User deletion failed: " + ex.Message);
            }
        }

        public static void ChangePassword(string UserName, string OldPassword, string NewPassword)
        {
            MembershipProvider defaultProvider;
            bool bChanged = false;

            try
            {
                defaultProvider = Membership.Provider;

                if (defaultProvider.EnablePasswordReset)
                {
                    bChanged = defaultProvider.ChangePassword(UserName, OldPassword, NewPassword);
                    if (bChanged == false) throw new Exception("This password could not be changed.");
                }
                else
                {
                    // This provider is not configured to let users change their passwords
                    throw new Exception("You do not have permissions to change your password.");
                }
            }
            catch (Exception ex)
            {
                throw new Exception("Your password was not changed: " + ex.Message);
            }

        }

        /// <summary>Get the Membership provider name from the web.config file</summary>
        /// <returns>Name of the membership provider</returns>
        public static string GetMembershipProviderName()
        {
            string providerName = string.Empty;

            // Get the provider name from the current Membership object
            providerName = Membership.Provider.Name;

            // Return the provider name
            return providerName;
        }

    }
}
